package com.hqyj.javaSpringBoot.modules.account.dao;

import com.hqyj.javaSpringBoot.modules.account.entity.Role;
import com.hqyj.javaSpringBoot.modules.common.entiy.SearchBean;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface RoleDao {

    @Insert("insert into account_role (create_date,role_name) values" +
            " (#{createDate},#{roleName})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void insertRole(Role role);

    @Select("select * from account_role where role_name=#{roleName}")
    List<Role> getRoleByRoleName(String roleName);

    @Delete("delete from account_role where id=#{id}")
    void deleteRole(int id);

    @Update("update account_role set role_name=#{roleName} where id=#{id}")
    void updateRole(Role role);

    @Select("select * from account_role where id=#{id}")
    Role getRoleByRoleId(int id);

    @Select("select * from account_role ar left join account_user_role aur on ar.id = aur.role_id "
            + "where aur.user_id = #{userId}")
    List<Role> getRolesByUserId(int userId);


    @Select("select * from account_role ar left join account_role_resource arr on ar.id=arr.role_id" +
            " where arr.resource_id=#{resourceId}")
    List<Role> getRolesByResourceId(int resourceId);

    @Select("<script>" +
            "select * from account_role" +
            " <where>" +
                "<if test='keyWord !=\"\" and keyWord != null'>" +
                    " role_name like '%${roleName}%'" +
                "</if>" +
            " </where>" +
            "<choose>" +
                "<when test='orderBy !=\"\" and orderBy != null'>" +
                    " order by ${orderBy} ${direction}" +
                "</when>" +
                "<otherwise>" +
                    " order by id" +
                "</otherwise>" +
            "</choose>" +
            "</script>")
    List<Role> getRoleBySearch(SearchBean searchBean);

    /**
     * 查询所有角色
     * @return
     */
    @Select("select * from account_role")
    List<Role> getRoles();
}
